<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Base Helper class
 * @author cks
 */
class Lib_Rss
{
    
    public static function getRssInfo()
    {

        //$rssfeed = array("http://www.zhihu.com/rss");
        //$rssfeed = array("http://cn.engadget.com/rss.xml");
       // $rssfeed = array("http://tech.qq.com/web/webnews/rss_11.xml",'http://tech.qq.com/web/it/telerss.xml');
        //$rssfeed = array("http://tech.qq.com/web/dzsw/dzsw.xml");
        //$rssfeed = array("http://cn.engadget.com/rss.xml");http://www.geekpark.net/rss http://feed.iplaysoft.com/
        //$rssfeed = array("http://www.oschina.net/news/rss?show=industry");
        //$rssfeed = array("http://feed.cnblogs.com/news/rss");
        $rssfeed = array("http://www.geekpark.net/rss");
        for($i=0;$i<sizeof($rssfeed);$i++){//分解开始

            $buff = "";

            $rss_str="";

            //打开rss地址，并读取，读取失败则中止
            $fp = @fopen($rssfeed[$i],"r") or die("can not open $rssfeed"); 
            while ( !feof($fp) ) {

              $buff .= fgets($fp,4096);

            }

            //关闭文件打开    
            fclose($fp);
                
            //建立一个 XML 解析器
            $parser = xml_parser_create();

            //xml_parser_set_option -- 为指定 XML 解析进行选项设置
            xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);

            //xml_parse_into_struct -- 将 XML 数据解析到数组$values中
            xml_parse_into_struct($parser,$buff,$values,$idx);

            //xml_parser_free -- 释放指定的 XML 解析器
            xml_parser_free($parser);

            $num=1;

            $result=array();

            for($num=0;$num<100000;$num++){  

                $val=@$values[$num];

                $tag = $val["tag"];

                $type = $val["type"];

                $value =@ $val["value"];

                //标签统一转为小写
                $tag = strtolower($tag);
                
                if ($tag == "item" && $type == "open"){

                    $is_item = 1;

                }else if ($tag == "item" && $type == "close") {

                    $result[]=array(
                      'title'=>$title,
                      'link'=>$link,
                      'description'=>$description
                    );
                    $is_item = 0;
                }

                //仅读取item标签中的内容
                if(@$is_item==1){

                    if ($tag == "title") {$title = $value;}   

                    if ($tag == "link") {$link = $value;}

                    if ($tag == "description") {$description = $value;}

                }

            }

            return $result;
        }
    }
    public static function save(){

        $rss=Lib_Rss::getRssInfo();

        foreach ($rss as $rkey => $rlist) {

             $sql="select count(1)total
             from news t
             where title='".$rlist['title']."'
             and description='".$rlist['description']."'
             ";

            $total=DB::query(Database::SELECT,$sql)->execute()->get("total");

            if($total==0){

                 list($insert_id, $total_rows)= DB::insert('news')

                ->columns(array('title','description','createdon','link'))
                
             ->values(array($rlist['title'],$rlist['description'],date("Y-m-d h:i:sa"),$rlist['link']))->execute();

           
            }
        }

    }
}
